//
//  LoginViewViewController.m
//  WULIHH
//
//  Created by tang on 16/4/12.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "LoginViewViewController.h"
#import "Header.h"
#import "UMSocial.h"


#import "WiLiLoginRequest.h"

@interface LoginViewViewController ()
@property (nonatomic,strong)UILabel *titlelabel;
@property (nonatomic,strong)UIImageView *shutDownImageView;
@property (nonatomic,strong)UIButton *shutDownBtn;
@property (nonatomic,strong)UILabel *waylabel;

@property (nonatomic,strong)UILabel *promptlabel;
@property (nonatomic,strong)UIButton *promptBtn;
@end

@implementation LoginViewViewController


- (void)loadView
{
    [super loadView];
    
    [self.navigationController setNavigationBarHidden:YES];
    
    UIImage *image = nil;
    
    image = [UIImage imageNamed:@"background.png"];
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WithView, heightView)];
    [self.view addSubview:backImageView];
    [backImageView setImage:image];
    
    image = [UIImage imageNamed:@"wechatdenglu"];
    UIButton *wechatimageView = [[UIButton alloc] initWithFrame:CGRectMake((WithView-image.size.width)/2.0, 211.0, image.size.width, image.size.height)];
    [wechatimageView setBackgroundImage:image forState:UIControlStateNormal];
    [self.view addSubview:wechatimageView];
    
    image = [UIImage imageNamed:@"qqdenglu"];
    UIButton *qqimageView = [[UIButton alloc] initWithFrame:CGRectMake((WithView-image.size.width)/2.0, CGRectGetMaxY(wechatimageView.frame)+14.0, image.size.width, image.size.height)];
    [qqimageView setBackgroundImage:image forState:UIControlStateNormal];
    [self.view addSubview:qqimageView];
    
    image = [UIImage imageNamed:@"xinlangdenglu"];
    UIButton *sinaimageView = [[UIButton alloc] initWithFrame:CGRectMake((WithView-image.size.width)/2.0, CGRectGetMaxY(qqimageView.frame)+15.0, image.size.width, image.size.height)];
    [sinaimageView setImage:image forState:UIControlStateNormal];
    [self.view addSubview:sinaimageView];
    
    
    [wechatimageView setTag:1];
    [qqimageView setTag:2];
    [sinaimageView setTag:3];
    
    [wechatimageView addTarget:self action:@selector(loginButton:) forControlEvents:UIControlEventTouchUpInside];
    [qqimageView addTarget:self action:@selector(loginButton:) forControlEvents:UIControlEventTouchUpInside];
    [sinaimageView addTarget:self action:@selector(loginButton:) forControlEvents:UIControlEventTouchUpInside];
    
    _titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(wechatimageView.frame), 146.5, 73, 21)];
    [self.view addSubview:_titlelabel];
    
    _shutDownImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(wechatimageView.frame) - 30.0, 146.5, 22.5, 22.5)];
    
    [self.view addSubview:_shutDownImageView];
    
    _shutDownBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(wechatimageView.frame)-30.0, 146.5, 30, 30)];
    
    [self.view addSubview:_shutDownBtn];
    
    _waylabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(wechatimageView.frame) , 180.5, 158, 14)];
    [self.view addSubview:_waylabel];
    
    _promptlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(sinaimageView.frame), CGRectGetMaxY(sinaimageView.frame)+15.0, 145, 12)];
    [self.view addSubview:_promptlabel];
    
    _promptBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_promptlabel.frame), CGRectGetMaxY(sinaimageView.frame)+15.0, 74.5, 12.5)];
    [self.view addSubview:_promptBtn];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [_titlelabel setText:@"登录WULI"];
    [_titlelabel setTextColor:[UIColor whiteColor]];
    [_titlelabel setFont:[UIFont boldSystemFontOfSize:16.0]];
    
    [_shutDownImageView setImage:[UIImage imageNamed:@"guanbi"]];
    
//    [_shutDownBtn setBackgroundColor:[UIColor redColor]];
    [_shutDownBtn addTarget:self action:@selector(shutDownEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    [_waylabel setText:@"您可以使用以下方式登录"];
    [_waylabel setFont:[UIFont systemFontOfSize:14.0]];
    [_waylabel setTextColor:[UIColor whiteColor]];
    
    [_promptlabel setText:@"登录后意味着你同意wuli的"];
    [_promptlabel setTextColor:[UIColor whiteColor]];
    [_promptlabel setFont:[UIFont systemFontOfSize:12.0]];
    
    [_promptBtn setTitle:@"《使用协议》" forState:UIControlStateNormal];
    [_promptBtn setTitleColor:uicolor(56, 187, 52) forState:UIControlStateNormal];
    [_promptBtn.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
    [_promptBtn addTarget:self action:@selector(promptEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLoginSuccess) name:yonghudenglu object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)loginButton:(UIButton *)sender
{
    if (sender.tag == 1)
    {
        
    }
    else if (sender.tag == 2)
    {
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            //          获取微博用户名、uid、token等
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
                
                
                [WiLiLoginRequest requestUserLogin:snsAccount.usid withUserName:snsAccount.userName withImage:snsAccount.iconURL withPlatForm:@"qq"];
                
                [[NSUserDefaults standardUserDefaults] setValue:snsAccount.usid forKey:@"openid"];
                [[NSUserDefaults standardUserDefaults] setValue:snsAccount.userName forKey:@"name"];
                [[NSUserDefaults standardUserDefaults] setValue:snsAccount.iconURL forKey:@"touxiang"];
                
//                NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
                
                
            }});

    }
    else if (sender.tag == 3)
    {
        
    }
}


//用户登录成功
- (void)userLoginSuccess
{
    [[NSUserDefaults standardUserDefaults] setValue:@"yes" forKey:userLogin];
    [self removeFromParentViewController];
    [self.view removeFromSuperview];

}



// 关闭按钮事件
- (void)shutDownEvent:(id)sender{
    
    
    [self removeFromParentViewController];
    [self.view removeFromSuperview];
    
}
// 协议事件
- (void)promptEvent:(id)sender{
    
    
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
